package cn.dansj.handler.interfaces;

import cn.dansj.annotation.MarkAsApi;
import cn.dansj.annotation.InterfaceParam;
import cn.dansj.handler.InterfaceHandler;
import cn.dansj.utils.interfaces.InterfaceUtils;
import cn.dansj.utils.project.ProjectUtils;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@SuppressWarnings("all")
@MarkAsApi
public class SendMailCaptcha extends InterfaceHandler {
    @Override
    @InterfaceParam(require = true, value = "mail", comment = "邮箱地址")
    @InterfaceParam(require = true, value = "use", comment = "用途")
    @InterfaceParam(value = "expire", comment = "过期时间(分钟)", defaultValue = "5")
    public Object execute(Map<String, Object> paramsMap) {
        try {
            String randomNumber = ProjectUtils.generateRandomNumber(4);
            String mail = paramsMap.get("mail").toString();
            String use = paramsMap.get("use").toString();
            int expire = Integer.parseInt(paramsMap.get("expire").toString());
            redisUtils.set("captcha@" + mail + "@" + use, randomNumber, expire, TimeUnit.MINUTES);
            ProjectUtils.sendCaptcha(mail, randomNumber, String.valueOf(expire));
            return true;
        } catch (Exception e) {
            InterfaceUtils.logger.error(e.getMessage(), e);
            return false;
        }
    }
}